package org.sjm.samples.service;

import java.util.ArrayList;
import java.util.List;

import org.sjm.samples.dao.ISubjectDAO;
import org.sjm.samples.dao.IUserSubjectDAO;
import org.sjm.samples.model.ExamSubjectVO;
import org.sjm.samples.model.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class SubjectServiceImpl implements ISubjectService {

	@Autowired
	private ISubjectDAO subjectDAO;
	
	@Autowired
	private IUserSubjectDAO userSubjectDAO;
	
	@Override
	public List<ExamSubjectVO> findAllSubjects() {
		List<Subject> subjectList = subjectDAO.FindAllSubjects();
		List<ExamSubjectVO> voList = new ArrayList<ExamSubjectVO>();
		for(Subject subject : subjectList){
			ExamSubjectVO vo = new ExamSubjectVO();
			BeanUtils.copyProperties(subject, vo);
			voList.add(vo);
		}
		return voList;
	}

	@Override
	public ExamSubjectVO getSubjectById(Integer subId) {
		Subject subject = subjectDAO.getSubjectById(subId);
		ExamSubjectVO vo = new ExamSubjectVO();
		BeanUtils.copyProperties(subject, vo);
		return vo;
	}

	@Override
	public void editSubject(ExamSubjectVO subjectVO) throws Exception {
		Subject subject = new Subject();
		BeanUtils.copyProperties(subjectVO, subject);
		subjectDAO.editSubject(subject);
	}

	@Override
	public void createSubject(ExamSubjectVO subjectVO) throws Exception {
		Subject subject = new Subject();
		BeanUtils.copyProperties(subjectVO, subject);
		subjectDAO.createSubject(subject);
	}

	@Override
	public void deleteSubject(Integer subId) throws Exception {
		userSubjectDAO.deleteUserSubjectBySubId(subId);
		subjectDAO.deleteSubject(subId);
	}

	@Override
	public String editSubjectStatus(Integer subId) {
		Subject subject = subjectDAO.getSubjectById(subId);
		String message = "";
		if("0".equals(subject.getStatus())){
			subject.setStatus("1");
			message = "开启";
		}else{
			subject.setStatus("0");
			message = "关闭";
		}
		try {
			subjectDAO.editSubject(subject);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return message;
	}

	@Override
	public List<String> getAllUserSubject(Integer subId) {
		return subjectDAO.getAllUserSubject(subId);
	}

	@Override
	public void saveSubjectStudent(List<String> subjectStudents, Integer subId) throws Exception {
		List<String> dbSubjectUserList = subjectDAO.getAllUserSubject(subId);
		
		List<String> insertList = new ArrayList<String>();
		List<String> delList = new ArrayList<String>();
		
		for(String selectedUser : subjectStudents){
			if(!dbSubjectUserList.contains(selectedUser)){
				insertList.add(selectedUser);
			}
		}
		
		for(String selectedUser : dbSubjectUserList){
			if(!subjectStudents.contains(selectedUser)){
				delList.add(selectedUser);
			}
		}
		
		userSubjectDAO.insertSubjectUsers(insertList, subId);
		
		userSubjectDAO.deleteSubjectUsers(delList, subId);
	}

	@Override
	public void saveStudentSubject(List<Integer> studentSubjects, String userId) throws Exception {
		List<Integer> dbUserSubjectList = subjectDAO.getSubjectsByUserId(userId);
		
		List<Integer> insertList = new ArrayList<Integer>();
		List<Integer> delList = new ArrayList<Integer>();
		
		for(Integer selectedSubject : studentSubjects){
			if(!dbUserSubjectList.contains(selectedSubject)){
				insertList.add(selectedSubject);
			}
		}
		
		for(Integer selectedSubject : dbUserSubjectList){
			if(!studentSubjects.contains(selectedSubject)){
				delList.add(selectedSubject);
			}
		}
		
		userSubjectDAO.insertStudentSubjects(insertList, userId);
		
		userSubjectDAO.deleteStudentSubjects(delList, userId);
	}
	
	@Override
	public List<ExamSubjectVO> FindSubjectsByUserId(String userId) {
		List<Subject> subjectList = subjectDAO.FindSubjectsByUserId(userId);
		List<ExamSubjectVO> voList = new ArrayList<ExamSubjectVO>();
		for(Subject subject : subjectList){
			ExamSubjectVO vo = new ExamSubjectVO();
			BeanUtils.copyProperties(subject, vo);
			voList.add(vo);
		}
		return voList;
	}

	@Override
	public List<Integer> getSubjectsByUserId(String userId) {
		List<Integer> list = subjectDAO.getSubjectsByUserId(userId);
		if(list != null){
			return list;
		}else{
			return null;
		}
	}

	@Override
	public List<Integer> getSubjectsByTest(String userId) {
		List<Integer> list = subjectDAO.getSubjectsByTest(userId);
		if(list != null){
			return list;
		}else{
			return null;
		}
	}
}
